package cc.iotkit.manager.controller;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.api.Request;
import cc.iotkit.common.api.Response;
import cc.iotkit.data.dao.DeviceInfoRepository;
import cc.iotkit.data.dao.WzrGroupDeviceInfoRepository;
import cc.iotkit.data.dao.WzrGwDevGroupRepository;
import cc.iotkit.data.dao.WzrGwDevRepository;
import cc.iotkit.data.model.TbDeviceInfo;
import cc.iotkit.data.model.TbWzrGroupDeviceInfo;
import cc.iotkit.data.model.TbWzrGwDev;
import cc.iotkit.data.model.TbWzrGwDevGroup;
import cc.iotkit.data.service.DeviceInfoDataImpl;
import cc.iotkit.manager.dto.bo.device.DevToGwBo;
import cc.iotkit.manager.dto.bo.device.DeviceInfoBo;
import cc.iotkit.manager.dto.bo.device.InGwDevBo;
import cc.iotkit.manager.dto.bo.devicegroup.WzrGroupBo;
import cc.iotkit.manager.service.WzrGwDevService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Api(tags = {"微自然控制器"})
@Slf4j
@RestController
@RequestMapping("/wzrgwdev")
public class WzrGwDevController {
   @Autowired
   private WzrGwDevService wzrGwDevService;
   @Autowired
   private WzrGwDevGroupRepository wzrGwDevGroupRepository;
   @Autowired
   private DeviceInfoRepository deviceInfoRepository;
   @Autowired
   private DeviceInfoDataImpl deviceInfoData;
   @Autowired
   private WzrGwDevRepository wzrGwDevRepository;
   @Autowired
   private WzrGroupDeviceInfoRepository wzrGroupDeviceInfoRepository;

    @ApiOperation(value = "分配设备到网关", notes = "网关", httpMethod = "POST")
    @PostMapping("/setdevtogw")
    public Response setGwDev(@RequestBody Request<DevToGwBo> request) {
         return wzrGwDevService.setDevToGw(request.getData().getGwid(), request.getData().getDeviceId(),request.getRequestId(), request.getData().getService());
    }
    @ApiOperation(value = "增加修改群组")
    @PostMapping("addgroup")
    public Response addGroup(@RequestBody Request<TbWzrGwDevGroup> request) {
        if (request.getData().getId() == 0){
            request.getData().setId(UUID.randomUUID().getMostSignificantBits());
            return new Response<>(200,"群组添加成功",wzrGwDevGroupRepository.saveAndFlush(request.getData()),request.getRequestId());
        }
        if (request.getData().isDel()){
            wzrGwDevGroupRepository.deleteById(request.getData().getId());
            return new Response<>(200,"删除群组成功",null,request.getRequestId());
        }
        TbWzrGwDevGroup wzrGwDevGroup = wzrGwDevGroupRepository.findById(request.getData().getId()).orElse(null);
        wzrGwDevGroup.setGwDevGroupName(request.getData().getGwDevGroupName());
        return new Response<>(200,"群组名称修改成功",wzrGwDevGroupRepository.saveAndFlush(wzrGwDevGroup),request.getRequestId());
    }
    @ApiOperation("群组设备增删改和工作状态增删改")
    @PostMapping("adGroupDev")
    public Response addOrEdGroupDev(@RequestBody Request<WzrGroupBo> request) {
        return wzrGwDevService.addGroupDev(request.getData());
    }

    @ApiOperation("获取群组列表")
    @PostMapping("getGroupList")
    public Response getGroupList(@RequestBody PageRequest request) {
        Paging<TbWzrGwDevGroup> pt = wzrGwDevService.findAllByPagNative(request.getPageNum()-1,request.getPageSize());
        return new Response(200,"群组列表获取成功",pt,request.getRequestId());
    }
    @ApiOperation("获取全部的网关")
    @PostMapping("getGwAll")
    public Response<List<TbDeviceInfo>> getGwAll(@RequestBody Request<DeviceInfoBo> request) {
        return new Response<>(200,"获取网关列表成功",deviceInfoRepository.findTbDeviceInfosByProductKey(request.getData().getProductKey()),request.getRequestId());
    }
    @ApiOperation("获取入网的设备")
    @PostMapping("getDeviceInGw")
    public Response getDeviceInGw(@RequestBody PageRequest<InGwDevBo> request) {
        //分页获得已入网的设备
        if (request.getData().isIngw()){
            Paging<TbDeviceInfo>  paging =deviceInfoData.findDeviceByInOutGw(request.getData().isIngw(),request.getPageNum(), request.getPageSize(), wzrGwDevRepository.findAll().stream().map(TbWzrGwDev::getDeviceId).collect(Collectors.toList()),request.getData().getGwname(),request.getData().getGwid());
            return new Response<>(200,"获取设备列表成功",paging,request.getRequestId());
        }
        //分页获得未入网设
        Paging<TbDeviceInfo>  paging =deviceInfoData.findDeviceByInOutGw(false,request.getPageNum(), request.getPageSize(), wzrGwDevRepository.findAll().stream().map(TbWzrGwDev::getDeviceId).collect(Collectors.toList()),request.getData().getGwname(),request.getData().getGwid());
        return new Response<>(200,"获取设备列表成功",paging,request.getRequestId());
    }
    @ApiOperation("根据群组获取设备")
    @PostMapping("getDeviceByGroup")
    public Response<List<TbDeviceInfo>> getDeviceByGroup(@RequestBody Request<WzrGroupBo> request) {
        List<TbWzrGroupDeviceInfo> ids = wzrGroupDeviceInfoRepository.findAllByGroupId(String.valueOf(request.getData().getGroupid()));
        List<String> dids = ids.stream().map(TbWzrGroupDeviceInfo::getDevid).collect(Collectors.toList());
        List<TbDeviceInfo> deviceInfoList = deviceInfoRepository.findAllById(dids);
        long total = deviceInfoList.size();
        return new Response<>(200,"获取设备列表成功",new Paging<>(total, deviceInfoList),request.getRequestId());
    }
}